package activity

import (
	"gitee.com/golang-module/carbon"
	"go.mongodb.org/mongo-driver/bson"
	"server/db"
)

const (
	TBL_ACTIVITY = "t_activity"
)

type ActivityDao struct {
	db *db.MongoMiddleware
}

func NewActivityDao() *ActivityDao {
	return &ActivityDao{
		db: db.Db,
	}
}
func (a *ActivityDao) Insert(data Activity) error {
	return a.db.Insert(TBL_ACTIVITY, data)
}
func (a *ActivityDao) Update(userId string, updateDataKey string, updateData interface{}) error {
	filter := bson.M{"user_id": userId}
	update := bson.M{"$set": bson.M{updateDataKey: updateData}}
	return a.db.Update(TBL_ACTIVITY, filter, update)
}
func (a *ActivityDao) QianDao(userId string, times int) error {
	filter := bson.M{"user_id": userId}
	update := bson.M{"$set": bson.M{"times": times, "last_qian_dao_time": carbon.Now().Timestamp()}}
	return a.db.Update(TBL_ACTIVITY, filter, update)
}
func (a *ActivityDao) GetInfo(userId string) (Activity, error) {
	var info Activity
	filter := bson.M{"user_id": userId}
	err := a.db.FindOne(TBL_ACTIVITY, filter, &info)
	if err != nil {
		return info, err
	}
	return info, nil
}
